package bjx.com.kehu.ui.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import bjx.com.kehu.R;
import bjx.com.kehu.contants.Constants;
import bjx.com.kehu.listener.MyDigitsKeyListener;
import bjx.com.kehu.utils.GsonUtil;
import bjx.com.kehu.utils.MD5;
import bjx.com.kehu.utils.SPUtils;
import bjx.com.kehu.utils.Util;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by jqc on 2017/10/20.
 */

public class LoginActivity extends BaseActivity {

    public static LoginActivity mInstants;

    EditText mETUName;
    EditText mETUPwd;
    TextView mTVForgetPwd;
    TextView mTVLogin;
    TextView mTVReg;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        mInstants = this;

        mETUName = getViewById(R.id.login_et_uname);
        Drawable drawable = getResources().getDrawable(R.mipmap.user);
        drawable.setBounds(0, 0, Util.dip2px(mApp, 20), Util.dip2px(mApp, 20));
        mETUName.setCompoundDrawables(drawable, null, null, null);
        mETUPwd = getViewById(R.id.login_et_upwd);
        Drawable drawable2 = getResources().getDrawable(R.mipmap.pwd);
        drawable2.setBounds(0, 0, Util.dip2px(mApp, 20), Util.dip2px(mApp, 20));
        mETUPwd.setCompoundDrawables(drawable2, null, null, null);
        mTVForgetPwd = getViewById(R.id.login_tv_fpwd);
        mTVLogin = getViewById(R.id.login_tv_login);
        mTVReg = getViewById(R.id.login_tv_reg);
    }

    @Override
    protected void setListener() {
        mTVForgetPwd.setOnClickListener(this);
        mTVLogin.setOnClickListener(this);
        mTVReg.setOnClickListener(this);
        mETUName.setKeyListener(new MyDigitsKeyListener(mApp));
        mETUPwd.setKeyListener(new MyDigitsKeyListener(mApp));
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.login_tv_fpwd:
//                startActivity(new Intent(mApp, ForgetPwdActivity.class).addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));

                break;
            case R.id.login_tv_login:
                String username = mETUName.getText().toString().trim();
                String password = mETUPwd.getText().toString().trim();

                if(username == null || username.equals("")) {
                    showToast("用户名不能为空");
                    mETUName.requestFocus();
                    return;
                }
                if(!Util.isMobileNO(username)) {
                    showToast("用户名不正确");
                    mETUName.requestFocus();
                    return;
                }

                if(password == null || password.equals("")) {
                    showToast("密码不能为空");
                    mETUPwd.requestFocus();
                    return;
                }
                if(password.length() < 5) {
                    showToast("密码长度必须是5-12位");
                    mETUPwd.requestFocus();
                    return;
                }

                mEngine.login(username, password, String.valueOf(SPUtils.get(mApp, Constants.CHANNELID, "-1")), MD5.getMessageDigest((username + Constants.BASE_KEY + password).getBytes())).enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        try {
                            String str = response.body().string();
                            JSONObject jo = new JSONObject(str);
                            int status = jo.getInt("status");
                            String msg = jo.getString("msg");

                            if (status == 200) {
                                showToast("登录成功");
                                SPUtils.saveObject(mApp, "usermodel", GsonUtil.jsonToUserBean(jo.getString("result")));
                                finish();
                            } else if (status == 1080) {
                                showToast("用户不存在");
                            } else if (status == 1081) {
                                showToast("用户名或密码不正确");
                            } else if (status == 106) {
                                showToast("加盟商不存在");
                            } else if (status == 100) {
                                showToast("登录失败");
                            } else {
                                showToast("登录异常");
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                    }
                });

                break;
            case R.id.login_tv_reg:
                startActivity(new Intent(mApp, RegisterActivity.class).addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
                break;
            default:
                break;
        }
    }
}
